import os
import abc

from unittest.mock import patch
from library_test.base_test import BaseLibraryTest


class TestCheckForInstall(BaseLibraryTest, metaclass=abc.ABCMeta):
    TESTCASE_DIR = os.path.join(os.path.dirname(__file__), "testcase")

    @classmethod
    def get_module_path(cls):
        return "ascend_deployer.library.check_for_install"

    @classmethod
    def setUpClass(cls) -> None:
        cls.replace_linux_module()
        cls.replace_ansible_module()
        super().setUpClass()

        from ascend_deployer.library.check_for_install import CompatibilityCheck
        cls._mock_get_npu_info()
        cls._mock_get_os_and_arch()
        cls._mock_get_card()
        cls.compatibility_check = CompatibilityCheck()

    @classmethod
    def _replace_class(cls):
        pass

    @classmethod
    def _mock_get_npu_info(cls):
        patcher = patch(cls.get_module_path() + ".get_npu_info")
        mocker = patcher.start()
        mocker.return_value = {"card": "Atlas 900 A3 Pod"}

    @classmethod
    def _mock_get_os_and_arch(cls):
        patcher = patch(cls.get_module_path() + ".get_os_and_arch")
        mocker = patcher.start()
        mocker.return_value = {"EulerOS_2.10_aarch64"}

    @classmethod
    def _mock_get_card(cls):
        patcher = patch(cls.get_module_path() + ".util.get_card")
        mocker = patcher.start()
        mocker.return_value = "910_93"

    @classmethod
    def get_testcase_path(cls):
        return os.path.join(cls.TESTCASE_DIR, "check_for_install.yml")

    def test_check_card(self):
        self.compatibility_check.error_messages = []
        self.compatibility_check.npu_info = {"card": "Atlas 900 A3 Pod"}
        self.compatibility_check.os_and_arch = "EulerOS_2.10_aarch64"
        self.compatibility_check.check_card()
        self.assertTrue("Check card failed" in self.compatibility_check.error_messages[0])

        # check success
        self.compatibility_check.error_messages = []
        self.compatibility_check.os_and_arch = "Ubuntu_22.04_aarch64"
        self.compatibility_check.check_card()
        self.assertEqual(0, len(self.compatibility_check.error_messages))

        # Non-exists card
        self.compatibility_check.error_messages = []
        self.compatibility_check.npu_info = {"card": "Non-exist"}
        self.compatibility_check.check_card()
        self.assertTrue("Check card failed" in self.compatibility_check.error_messages[0])
